package com.yqfk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqfk.auto.JSONAuthentication;
import com.yqfk.entity.YMap;
import com.yqfk.entity.YUser;
import com.yqfk.mapper.YMapMapper;
import com.yqfk.response.Result;
import com.yqfk.utils.BaiDuApiUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yqfk
 * @since 2022-04-07
 */
@RestController
@RequestMapping("/y-map")
public class YMapController extends JSONAuthentication {
    @Autowired
    YMapMapper yMapMapper;
    @ApiOperation(value = "查找所有地区")
    @PostMapping("/findAllMap")
    public void findAllMap(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String,Object> map=new HashMap<>();
        List<YMap> yMaps = yMapMapper.selectList(null);
        for (YMap a : yMaps){
           map.put(a.getName(),BaiDuApiUtils.getCoordinate(a.getName()));
        }

        if(yMaps.size()>0){
            Result ok = Result.ok().data("map", yMaps).data("jwd",map);
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error().message("未查到数据");
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "修改地区的感冒人数")
    @PostMapping("/UpdMap")
    public void UpdMap(HttpServletRequest request, HttpServletResponse response,@RequestParam("mapName") String name,@RequestParam("num") int num) throws Exception {
      QueryWrapper wrapper=new QueryWrapper();
      wrapper.eq("name",name);
        YMap yMap = yMapMapper.selectOne(wrapper);
        yMap.setName(name);
        yMap.setNum(num);

        if(yMapMapper.updateById(yMap)>0){
            Result ok = Result.ok().message("修改成功");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "添加地区")
    @PostMapping("/addMapAddress")
    public void addMapAddress(HttpServletRequest request, HttpServletResponse response,@RequestParam("address") String address,@RequestParam("num") int num) throws Exception {
        double[] coordinate = BaiDuApiUtils.getCoordinate(address);
        if (coordinate.length>0){
        YMap yMap=new YMap();
        yMap.setName(address);
        yMap.setNum(num);
        if(yMapMapper.insert(yMap)>0){
            Result ok = Result.ok().message("添加成功");
            this.WriteJSON(request, response, ok);
        }else{
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
        }else{
            Result error = Result.error().message("请输入正确的地址");
            this.WriteJSON(request, response, error);
        }

    }
}

